package cn.ztydata.teachingsystem.heaven.dao;

import cn.ztydata.teachingsystem.heaven.entity.exercise.ExerciseTip;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 习题提示dao
 * Created by wyh on 2015/2/6.
 */
@Repository
public class ExerciseTipDao extends BaseDao {

    /**
     * 批量保存习题提示
     * @param exerciseId  习题Id
     * @param tips 提示集合
     * @return int[]
     *
     * @author wyh
     * @since 2015-2-7
     */
    public int[] saveTips(long exerciseId, List<ExerciseTip> tips){
        String sql = "INSERT INTO t_exercise_tip VALUES(NULL,"+exerciseId+",:tip)";

        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(tips.toArray());

        log.debug("批量保存提示");
        return namedParameterJdbcTemplate.batchUpdate(sql,batch);
    }


    /**
     * 获取习题提示
     * @param exerciseId
     * @return
     * @author lyj
     * @since 2015-2-9
     */
    public List<ExerciseTip> getExerciseTips(final long exerciseId){
        String sql = "SELECT id,sTip FROM t_exercise_tip WHERE iExerciseId = :iExerciseId";

        MapSqlParameterSource param = new MapSqlParameterSource().addValue("iExerciseId", exerciseId);

        log.debug("获取习题提示");
        return namedParameterJdbcTemplate.query(sql, param, new RowMapper<ExerciseTip>() {
            @Override
            public ExerciseTip mapRow(ResultSet resultSet, int i) throws SQLException {
                ExerciseTip exerciseTip = new ExerciseTip();

                exerciseTip.setId(resultSet.getLong("id"));
                exerciseTip.setTip(resultSet.getString("sTip"));

                return exerciseTip;
            }
        });
    }
}
